package com.service;

import com.dao.BoneTumorDao;
import com.dao.UserDao;
import com.entity.BoneTumor;
import com.util.FileUtil;
import com.util.excelUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wlc on 2018/5/31.
 */
public class BoneTumorOperator {

//    public static String getSearchBoneTumor(String select_data_id) {
//        /**
//         * 获取选择后的骨肿瘤随访数据
//         */
//        ArrayList<ArrayList<String>> search_result = BoneTumorDao.load_data();
//        ArrayList<ArrayList<String>> select_result = new ArrayList<>();
//
//        for (ArrayList<String> result : search_result){
//            if (select_data_id.contains(result.get(0))){
//                select_result.add(result);
//            }
//        }
//        ArrayList<String> select_result_str = new ArrayList<>();
//        for (ArrayList<String> select_result_row : select_result){
//            select_result_str.add(StringUtils.join(select_result_row.toArray(),"lxyniubi"));
//        }
//        return StringUtils.join(select_result_str.toArray(),"wlcniubi");
//    }


    /**
     *  根据关键字查找骨肿瘤随访数据
     * @param user_cookie_value
     * @param projectPath
     * @param base
     * @param keyword
     * @return
     * @throws IOException
     */
    public static String setSearchResult(String user_cookie_value,String projectPath,String base,String keyword) throws IOException{
        String user_file_Path = projectPath+"/file_data/user/"+user_cookie_value.split("wlcniubi")[0]+"/";
        ArrayList<ArrayList<String>> search_result;

        if (base.equals("全部数据")) {
            search_result = BoneTumorDao.load_all_data();
        } else {
            search_result = BoneTumorDao.searchQM_data_by_keyword(base,keyword);
        }
        UserDao.saveUserSearchResult(search_result,user_file_Path+"searchResult");

        String last_result = "";
        if (search_result.size()>0){
            ArrayList<String> process_result = new ArrayList<>();
            //查找内容显示
            for (ArrayList<String> search_result_row : search_result){
                String[] show_content = {search_result_row.get(0), search_result_row.get(2), search_result_row.get(3), search_result_row.get(5), search_result_row.get(20)};
                process_result.add(StringUtils.join(show_content,"lxyniubi"));
            }
            last_result = StringUtils.join(process_result.toArray(),"wlcniubi");
        }
        last_result = last_result.replaceAll("\"|'|\r\n|\r|\n|\\s*", "");
        return last_result;
    }


//    /**
//     * 查询数据操作,查询全部随访数据
//     * @return 格式化数据
//     */
//    public static String searchBoneTumor() {
//
//        String format_data = "";
//        ArrayList<ArrayList<String>> data_array = BoneTumorDao.load_all_data();
//        if(data_array.size() > 0) {
//            ArrayList<String> temp = new ArrayList<>();
//            for(ArrayList<String> list :data_array) {
//                String[] show_content = {list.get(0), list.get(1), list.get(3), list.get(4)};
//                temp.add(StringUtils.join(show_content,"lxyniubi"));
//            }
//            format_data = StringUtils.join(temp.toArray(), "wlcniubi");
//        }
//        return format_data;
//    }

    /**
     * 将骨肿瘤表单处理后存储到数据库
     * @param part1
     * @param part2
     * @param part3
     * @param part4
     * @param part5
     */
    public static void saveBoneTumorForm(String part1,String part2,String part3,String part4,String part5, String[] file_names){
        BoneTumor boneTumor = new BoneTumor();
//      part1
        boneTumor.setBoneTumor_User_Information(part1);
//      part2
        boneTumor.setBoneTumor_Visit_Information(part2);
//      part3

        //part3插入影像文件地址
        List<String> temp = new ArrayList<String>(Arrays.asList(part3.split("wlcniubi")));
        for(int i = 0; i < file_names.length; i++) {
            String tmp = file_names[i].equals("null")?"null":file_names[i];
            temp.add(2+i, tmp);
        }
        part3 = String.join("wlcniubi", temp);

        boneTumor.setBoneTumor_Check_Information(part3);


//      part4
        boneTumor.setBoneTumor_Surgery_Information(part4);
//      part5
        boneTumor.setBoneTumor_Follow_Information(part5);

        BoneTumorDao.data_save(boneTumor);
    }


    /**
     *  读取数据(待重构)
     * @param timeId
     * @param path
     * @param file_name
     */
    public static boolean Analysis_bone_tumor(String timeId, String path, String file_name) {

        String abs_path = path + file_name;

        ArrayList<String> user_information = new ArrayList<String>(){{ add(timeId); }};
        ArrayList<String> visit_information = new ArrayList<String>(){{ add(timeId); }};
        ArrayList<String> check_information = new ArrayList<String>(){{ add(timeId); }};
        ArrayList<String> surgery_information = new ArrayList<String>() {{ add(timeId); }};
        ArrayList<String> followUp_information = new ArrayList<String>() {{ add(timeId); }};

        try {
            InputStream in = new FileInputStream(new File(abs_path));
            XSSFWorkbook wb = new XSSFWorkbook(in);
            XSSFSheet sheet =  wb.getSheetAt(0);
            XSSFRow row = null;
            XSSFCell cell = null;
            //患者信息
            row = sheet.getRow(4);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                user_information.add(temp);
            }
            //首次就诊信息
            row = sheet.getRow(7);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                visit_information.add(temp);
            }
            row = sheet.getRow(9);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                visit_information.add(temp);
            }
            //首次检查信息
            row = sheet.getRow(12);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                check_information.add(temp);
            }
            int image_count = 7;
            for(int i = 0; i < image_count; i++) {
                check_information.add(2, "null");
            }

            //手术信息
            row = sheet.getRow(15);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                surgery_information.add(temp);
            }

            //手术植入材料跳页
            sheet = wb.getSheetAt(1);
            //植入材料
            for(int i = 2; i <= 16; i++) {
                if(i == 9 || i == 10) continue;
                row = sheet.getRow(i);
                cell = row.getCell(2);
                String vendor = excelUtil.getCellStrValue(cell);
                surgery_information.add(vendor);
                cell = row.getCell(3);
                String model = excelUtil.getCellStrValue(cell);
                surgery_information.add(model);
            }

            sheet = wb.getSheetAt(0);

            //术中情况
            row = sheet.getRow(20);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                surgery_information.add(temp);
            }
            row = sheet.getRow(23);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                surgery_information.add(temp);
            }
            //随访信息
            row = sheet.getRow(26);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                followUp_information.add(temp);
            }
            row = sheet.getRow(28);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                followUp_information.add(temp);
            }
            row = sheet.getRow(30);
            for(int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                cell = row.getCell(i);
                String temp = excelUtil.getCellStrValue(cell);
                followUp_information.add(temp);
            }
            // 去掉图片字段
            for(int i = 0; i < image_count;i ++) {
                followUp_information.add(3, "null");
            }
            // close file stream
            in.close();

//            System.out.println(followUp_information);

            //excel每个文件只存一个随访信息
            ArrayList<ArrayList<String>> followUp_informations = new ArrayList<>();
            followUp_informations.add(followUp_information);

            BoneTumor boneTumor = new BoneTumor(user_information, visit_information, check_information, surgery_information, followUp_informations);
            if(!BoneTumorDao.data_save(boneTumor)) {
                System.out.println("file :" + file_name + " input error ");
                return false;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }



}
